package com.demo.bmsconsole;

import java.util.Scanner;

import android.content.Context;
import android.util.Log;

// This class encapsulates commands to the Controllers.
// i.e. the protocol for transmission/reception.
public class Interpreter
{
	private Context mCtx;
	private static String TAG;

	Interpreter(Context ctx)
	{
		mCtx = ctx;
		TAG = mCtx.getPackageName();
		initialize();
	}

	public void initialize()
	{
	}

	public static void handle(String line)
	{	
		Log.d(TAG, line);
		LogFile.log(line);
		Scanner scanner = new Scanner(line);
		if (!scanner.hasNext()) {
			return;
		}
		String cmd = scanner.next();
		if (cmd.equalsIgnoreCase("<count>")) {
			try {
				int count = scanner.nextInt();
				State.setCellCount(count);
			} catch(Exception e) {
				Log.e(TAG, "<count>: Bad protocol\n");
			}
		}
		else if (cmd.equalsIgnoreCase("<cells>")) {
			while (scanner.hasNext()) {	
				int cell = scanner.nextInt();
				float val = scanner.nextFloat();
				Log.d(TAG, Integer.toString(cell) + ":" + Float.toString(val) + "\n");
				//writeDB(cell,val);
			}
		}
		LogFile.log(line+"\n");
	}

	public static boolean setControllerConfig()
	{
		return (ADBServer.send("<bms> <tc> " + 
					Config.UVSet + " " + 
					Config.UVClear + " " + 
					Config.OVSet + " " +
					Config.OVClear) 
					&& 
		ADBServer.send("<bms> <ccc> " + Config.CellCheckDuration));
	}

	public static void beginI2Cinit() {
		ADBServer.send("<bms> <init>");
	}

}